/**
 * @Description: 大数据分析工具
 * @Date: 2020-03-13 16:42:18
 */
$(function () {
    //function getQueryVariable(variable) {
    //    var query = window.location.search.substring(1);
    //    var vars = query.split("&");
    //    for (var i = 0; i < vars.length; i++) {
    //        var pair = vars[i].split("=");
    //        if (pair[0] == variable) {
    //            return pair[1];
    //        }
    //    }
    //    return (false);
    //}
    //获取地址栏参数
    function getQueryVariable(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var param = window.location.search.substr(1);
        var r = param.match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    var spatial = getQueryVariable('spatial');
    var special=null;
    //if (special) {
    //    special = decodeURIComponent(special);
    //    $('.myTitle').html(special);
    //}

    // 重置窗口大小
    function resizeWindow() {
        var DEBUG;
        //DEBUG = true;
        if (DEBUG) {
            [].forEach.call($($('*')), function (a) {
                a.style.outline = '1px solid #' + (~~(Math.random() * (1 << 24))).toString(16);
            });
        }
        //console.log('resize...');
        var minHeight = $(window).height() - $('#DataCatalog').offset().top - 20;
        $('#DataCatalog').css('height', minHeight);
        $('.swiperTable tr').css('height', minHeight / 2 - 50);
    }
    resizeWindow();
    // resize窗口
    window.onresize = function () {
        resizeWindow();
    };

    function getData(value,spatialValue) {
        $.ajax({
            type: 'post',
            async: false,
            url: '/BaseAshx/Resource/ResourceAshx.ashx',
            // mock假数据
            // url: 'http://rest.apizza.net/mock/4b2127afb5657f4f96cf36d103057d12/BaseAshx/Resource/ResourceAshx.ashx',
            data: {
                stype: 'getResourceTools',
                orgid: '101010',
                where: value || '',
                // spatial: '',
                spatial: spatialValue,
                start: '',
                end: '',
            },
            datatype: 'json',
            success: function (res) {
                // alert('为测试分页功能,当前页面数据均为假数据')
                if (typeof (res) == 'string') {
                    res = JSON.parse(res);
                }
                console.log(res);
                if (res.code == 200) {
                    var html = '';
                    var length = res.data.length;
                    var tableNum = Math.ceil(length / 8);
                    var tdHtml = `
                <td><div class="product-grid" style="visibility: hidden;">
                        <div class="product-image">
                            <a href="javascript:void(0);">
                                <img class="pic-1" src="/img/home/homeNew/service1-1.png">
                            </a>
                        </div>
                    </div></td>`;
                    while (tableNum) {
                        html += `
                    <div class="swiper-slide">
                        <table class="swiperTable"><tbody>
                            <tr>` + tdHtml + tdHtml + tdHtml + tdHtml + `</tr>
                            <tr>` + tdHtml + tdHtml + tdHtml + tdHtml + `</tr>
                        </tbody></table></div>`;
                        tableNum--;
                    }
                    $('.swiper-wrapper').html(html);
                    $('.swiperTable td').css('width', $('.swiperTable').width() / 4 - 5);
                    $('.swiperTable td').css('display', 'inline-block');
                    res.data.map(function (item, index) {
                        var PERMISSIONS = item.PERMISSIONS.split('');
                        html = `
                    <div class="product-grid">
                        <div class="product-image">
                            <div class="icon OrangeBack fa fa-wrench"></div>
                            <a href="javascript:void(0);">
                                <img class="pic-1" src="/img/BasePlatform/resource/` + item.ZYTB + `">
                            </a>
                        </div>
                        <div class="product-content">
                            <h3 class="title"><a href="#">` + item.NAME + `</a></h3>
                            <div class="price">
                                <i class="fa fa-eye"></i><span class="viewNum">` + item.VIEWNUM + `</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                <i class="fa fa-play-circle-o"></i><span class="runNum">` + item.RUNNUM + `</span>
                            </div>
                        </div>
                        <ul class="social" resourceID="` + item.RESOURCE_ID + `" position="`+ item.POSITION +`">
                            <li titleName="` + item.NAME + `" class="run"` + (item.PERMISSIONS[2] == '1' ? `` : ` style="display:none;"`) + `>
                                <a href="javascript:void(0)" data-tip="运行"><i class="fa fa-play-circle-o"></i></a>
                            </li>
                            <li class="edit" ` + (item.PERMISSIONS[1] == '1' ? `` : ` style="display:none;"`) + `>
                                <a href="javascript:void(0)" data-tip="编辑"><i class="fa fa-edit"></i></a>
                            </li>
                            <li class="add" ` + (item.PERMISSIONS[0] == '1' ? `` : ` style="display:none;"`) + `>
                                <a href="javascript:void(0)" data-tip="添加"><i class="fa fa-plus-square-o"></i></a>
                            </li>
                        </ul>
                    </div>`;
                        $('.swiper-wrapper td').eq(index).html(html);
                    });
                    var mySwiper = new Swiper('.swiper-container', {
                        loop: false,
                        pagination: {
                            el: '.swiper-pagination',
                            clickable: true,
                        },

                        navigation: {
                            nextEl: '.swiper-button-next',
                            prevEl: '.swiper-button-prev',
                        },
                    });
                    for (i = 0; i < mySwiper.pagination.bullets.length; i++) {
                        mySwiper.pagination.bullets[i].onmouseover = function () {
                            this.click();
                        };
                    }
                } else {
                    alert(res.msg);
                }
            },
            error: function (err) {
                console.log(err);
                // if (err.status == 405) {
                //     top.location.href = '/login.html?backurl=' + encodeURIComponent(top.location.href) + '&ishome=true'
                // }
            }
        });
    }
    switch (spatial) {
        case "ZT":
            spatial="GH|SP|JG|ZYPJ";
            special="专题应用服务";
            break;
        case "TY":
            spatial="TY";
            special="通用应用服务";
            break;
        case "ZHIT":
            spatial="ZT";
            special="专题图制作服务";
            break;
        case "YYZX":
            spatial="TY|ZT|GH|SP|JG|ZYPJ";
            special="应用中心";
            break;
        case "DSJ":
            spatial="TY";
            special="大数据分析";
            break;
        case "GH":
            spatial="GH";
            special="规划应用服务";
            break;
        case "SP":
            spatial="SP";
            special="审批应用服务";
            break;
        case "JG":
            spatial="JG";
            special="监管应用服务";
            break;
        case "ZYPJ":
            spatial = "ZYPJ";
            special="资源评价服务";
            break;
        default:
            spatial="ZT";
            special="专题图制作服务";
            break;
    }
    if (special) {
        $('.myTitle').html(special);
    }
    getData(null,spatial);

    // 点击运行
    $('#DataCatalog .run').on('click', function (e) {
        var $this = $(this);
        var text = $this.attr('titleName');
        var resourceid = $this.parent().attr('resourceid');
        if (window.parent.showModelDriven) {
            window.parent.showModelDriven(text, resourceid);
        } else if (window.showModelDriven) {
            window.showModelDriven(text, resourceid);
        } else {
            alert('模型启动失败');
        }
        $('.running').removeClass('running');
        $this.addClass('running');
        return;
        // alert('当前数据均为假数据');
        // var $this = $(this);
        // $.ajax({
        //     type: 'post',
        //     async: false,
        //     // mock数据
        //     url: 'http://rest.apizza.net/mock/4b2127afb5657f4f96cf36d103057d12/GetModelParameters',
        //     data: {},
        //     datatype: 'json',
        //     success: function (res) {
        //         if (typeof (res) == 'string') {
        //             res = JSON.parse(res);
        //         }
        //         console.log(res);
        //         if (res.code == 200) {
        //             var html = `
        //             <div id="form">
        //                 <div id="formContent">`;
        //             res.data.map(function (item, index) {
        //                 console.log(this);
        //                 html += `<div id="` + item.id + `" class="formItem" comment="` + item.comment + `">
        //                 <label class="formLabel">` + item.name + `</label>`;
        //                 switch (item.formtype) {
        //                     case "input":
        //                         html += `
        //                         <input class="formInput" type="text" value="` + item.value + `">`;
        //                         break;
        //                     case "option":
        //                         html += `
        //                         <select class="formSelect" name="" id="">`
        //                         item.option.map(function (ite) {
        //                             html += `<option value="` + ite + `">` + ite + `</option>`
        //                         });
        //                         html += `</select>`;
        //                         break;
        //                     case "radio":
        //                         html += `<ul>`;
        //                         item.option.map(function (ite) {
        //                             html += `<li>
        //                             <input class="radioInput" type="radio" name="` + item.ID + `" value="` + ite + `" title="` + ite + `">
        //                             <div class="radioDiv">` + ite + `</div>
        //                         </li>`;
        //                         });
        //                         html += `</ul>`;
        //                         break;
        //                     case "check":
        //                         html += `<ul>`;
        //                         item.option.map(function (ite) {
        //                             html += `<li>
        //                             <input class="radioInput" type="checkbox" name="` + item.ID + `" value="` + ite + `" title="` + ite + `">
        //                             <div class="radioDiv">` + ite + `</div>
        //                         </li>`;
        //                         });
        //                         html += `</ul>`;
        //                         break;
        //                     case "dataobject_local":
        //                         html += `<input class="formInput fileInput" type="file">`
        //                         break;
        //                     case "dataobject_sever":
        //                         html += `<input class="formInput fileInput" type="file">`
        //                         break;
        //                     default:
        //                         break;
        //                 }
        //                 html += `</div>`;
        //             });
        //             html += `</div>
        //             </div>
        //             <!-- 右侧 -->
        //             <div id="comment">
        //                 <div id="commentContent">
        //                     我是描述
        //                 </div>
        //             </div>`;
        //             var index = layer.open({
        //                 type: 1,
        //                 title: '运行{{模型名称}}',
        //                 content: html,
        //                 area: '700px',
        //                 shadeClose: true, //点击遮罩关闭
        //                 btn: ['运行', '取消'],
        //             });
        //             //layer.full(index);
        //             $('.formItem').hover(function (e) {
        //                 $('#commentContent').html($(e.currentTarget).attr('comment'));
        //             }, function () {
        //                 $('#commentContent').html('');
        //             });
        //         } else {
        //             layui.layer.msg(res.msg, {
        //                 shade: 0.3,
        //                 time: 1000 // 1秒关闭
        //             });
        //         }
        //         layer.closeAll('loading');
        //     },
        //     error: function (err) {
        //         console.log(err);
        //         layer.closeAll('loading');
        //     }
        // });
    });

     // 点击编辑
    $('#DataCatalog .edit').on('click', function (e) {
        var $this = $(this);
        var resourceId = $this.parent().attr('resourceid');
        var position = $this.parent().attr('position');
        // 就是浏览
        // window.parent.$('#fmain')[0].contentWindow.clickEdit(undefined,3,resourceId);
        window.parent.$('#fmain')[0].contentWindow.clickViewNew(resourceId, position, 3);
        var $viewNum = $this.parent().siblings('.product-content').find('.viewNum');
        var num = parseInt($viewNum.html()) + 1;
        $viewNum.html(num);
    });
     // 点击添加
    $('#DataCatalog .add').on('click', function (e) { 
        var $this = $(this);
        var resourceId = $this.parent().attr('resourceid');
        window.parent.$('#fmain')[0].contentWindow.clickAdddemo(undefined, undefined, resourceId);
    });

    // 点击搜索
    $('.RightButton').on('click', function () {
        var value = $('#ipt_searchSource').val();
        if (value) {
            getData(value,spatial);
        }
    });


    var setting = {
        view: {
            showTitle: false,
            dblClickExpand: false, //双击节点时，是否自动展开父节点的标识
            showLine: false, //是否显示节点之间的连线
            showIcon: true,
            fontCss: {
                'color': 'white',
                'font-weight': 'normal'
            }, //字体样式函数
            selectedMulti: false //设置是否允许同时选中多个节点
        },
        check: {
            //chkboxType: { "Y": "ps", "N": "ps" },
            chkStyle: "checkbox", //复选框类型
            enable: false //每个节点上是否显示 CheckBox 
        },
        data: {
            simpleData: { //简单数据模式
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: ""
            }
        },
        callback: {
            onClick: function (event, treeId, treeNode, e) {
                var text = $(event.target).text();
                console.log(treeNode);
                $('.formSelectTree').val(treeNode.name);
                $('.treeDiv').hide();
                $('#changeTreeInput').val('');
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            }
        }
    };
    var zNodes = [{
        "id": "101010",
        "name": "内蒙古自治区自然资源厅",
        "pId": "000000",
        "open": true,
        icon: "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/0.png"
    }, {
        "id": "10101001",
        "name": "不动产登记中心",
        "pId": "101010",
        icon: "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/0.png"
    }, {
        "id": "1010100101",
        "name": "信息科",
        "pId": "10101001"
    }, {
        "id": "1010100102",
        "name": "综合科",
        "pId": "10101001"
    }, {
        "id": "10101002",
        "name": "不动产登记局",
        "pId": "101010"
    }, {
        "id": "1010100201",
        "name": "法规科",
        "pId": "10101002"
    }, {
        "id": "1010100202",
        "name": "档案室",
        "pId": "10101002"
    }, {
        "id": "101010020201",
        "name": "查询科",
        "pId": "1010100202"
    }, {
        "id": "10101003",
        "name": "局长办公室",
        "pId": "101010"
    }, {
        "id": "1010100301",
        "name": "受理科",
        "pId": "10101003"
    }, {
        "id": "1010100302",
        "name": "中心领导室",
        "pId": "10101003"
    }, {
        "id": "1010100303",
        "name": "登记科",
        "pId": "10101003"
    }, {
        "id": "10101004",
        "name": "土地分局",
        "pId": "101010"
    }, {
        "id": "1010100401",
        "name": "审核科",
        "pId": "10101004"
    }, {
        "id": "10101005",
        "name": "副局长办公室",
        "pId": "101010"
    }, {
        "id": "10101006",
        "name": "123",
        "pId": "101010"
    }, {
        "id": "10101009",
        "name": "test机构简称",
        "pId": "101010"
    }, {
        "id": "10101010",
        "name": "test",
        "pId": "101010"
    }, {
        "id": "10101011",
        "name": "test机构简称",
        "pId": "101010"
    }, {
        "id": "1010101101",
        "name": "test机构简称",
        "pId": "10101011"
    }, {
        "id": "1010101102",
        "name": "11",
        "pId": "10101011"
    }, {
        "id": "1010101103",
        "name": "11",
        "pId": "10101011"
    }, {
        "id": "10101012",
        "name": "test机构简称",
        "pId": "101010"
    }, {
        "id": "1010101201",
        "name": "test机构简称",
        "pId": "10101012"
    }, {
        "id": "10101013",
        "name": "11",
        "pId": "101010"
    }, {
        "id": "1010101301",
        "name": "test机构简称",
        "pId": "10101013"
    }];
    var newNode = zNodes.map(function (item, index) {
        var level = (item.id.length - 6) / 2 - 0;
        var icon = '/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/'
        if (level >= 0 && level <= 4) {
            icon += level;
        } else {
            icon += '4';
        }
        icon += '.png';
        item.icon = icon;
        return item;
    });
    // console.log(JSON.stringify(newNode))
    zNodes = [{
        "id": "101010",
        "name": "内蒙古自治区自然资源厅",
        "pId": "000000",
        "open": true,
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/0.png"
    }, {
        "id": "10101001",
        "name": "不动产登记中心",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "1010100101",
        "name": "信息科",
        "pId": "10101001",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "1010100102",
        "name": "综合科",
        "pId": "10101001",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "10101002",
        "name": "不动产登记局",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "1010100201",
        "name": "法规科",
        "pId": "10101002",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "1010100202",
        "name": "档案室",
        "pId": "10101002",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "101010020201",
        "name": "查询科",
        "pId": "1010100202",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/3.png"
    }, {
        "id": "10101003",
        "name": "局长办公室",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "1010100301",
        "name": "受理科",
        "pId": "10101003",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "1010100302",
        "name": "中心领导室",
        "pId": "10101003",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "1010100303",
        "name": "登记科",
        "pId": "10101003",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "10101004",
        "name": "土地分局",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "1010100401",
        "name": "审核科",
        "pId": "10101004",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "10101005",
        "name": "副局长办公室",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "10101006",
        "name": "123",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "10101009",
        "name": "test机构简称",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "10101010",
        "name": "test",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "10101011",
        "name": "test机构简称",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "1010101101",
        "name": "test机构简称",
        "pId": "10101011",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "1010101102",
        "name": "11",
        "pId": "10101011",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "1010101103",
        "name": "11",
        "pId": "10101011",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "10101012",
        "name": "test机构简称",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "1010101201",
        "name": "test机构简称",
        "pId": "10101012",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }, {
        "id": "10101013",
        "name": "11",
        "pId": "101010",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/1.png"
    }, {
        "id": "1010101301",
        "name": "test机构简称",
        "pId": "10101013",
        "icon": "/MapServer/Images/Main/LayerManage-icon/LayerTree/16px/2.png"
    }]
    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    $('.formSelectTree').on('focus', function () {
        $('.treeDiv').show();
    });
    $('#changeTreeInput').on('keyup', function () {
        var txtObj = this;
        $('#treeDemo').show();
        if (txtObj.value.length > 0) {
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var nodeList = zTree.getNodesByParamFuzzy("name", txtObj.value);
            //将找到的nodelist节点更新至Ztree内
            $.fn.zTree.init($("#treeDemo"), setting, nodeList);
            if (nodeList.length == 0) {
                $('#treeDemo').hide();
            }
        } else {
            //隐藏树
            //$('#treeDemoDiv').hide();
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        }
    });
    //点击某个节点 然后将该节点的名称赋值值文本框
    function onClick(e, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        //获得选中的节点
        var nodes = zTree.getSelectedNodes(),
            v = "";
        //根据id排序
        nodes.sort(function compare(a, b) {
            return a.id - b.id;
        });
        for (var i = 0, l = nodes.length; i < l; i++) {
            if (i !== 0) {
                v += "," + nodes[i].name;
            }
            v += nodes[i].name;
        }
        //将选中节点的名称显示在文本框内
        var cityObj = $("#citySel");
        cityObj.attr("value", v);
        //隐藏zTree
        hideMenu();
        return false;
    }
});